package com.ruoyi.system.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.AlpContractDetail;
import com.ruoyi.system.domain.PivotRequest;
import com.ruoyi.system.service.IAlpAuthorityService;
import com.ruoyi.system.service.IAlpContractDetailService;
import com.ruoyi.system.service.IAlpWeeklyService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * @fileName: AlpWeeklyController
 * @description: TODO
 * @author: Li Wenbo
 * @date: 2020-3-2 14:46
 * @version: V1.0
 */
@Controller
@RequestMapping("/system/weekly")
public class AlpWeeklyController extends BaseController {

    private String prefix = "system/weekly";

    @Autowired
    private IAlpWeeklyService alpWeeklyService;
    @Autowired
    private IAlpContractDetailService alpContractDetailService;
    @Autowired
    private IAlpAuthorityService authorityFilter;

    @RequiresPermissions("system:weekly:view")
    @GetMapping()
    public String weeklyList(ModelMap mmap)
    {
        int week = DateUtils.getWeekOfYearByF(new Date(), 5);
        mmap.put("week", week);
        return prefix + "/weeklyList";
    }

    /**
     * 周汇总公司级
     */
    @RequiresPermissions("system:weekly:view")
    @PostMapping("/totalList")
    @ResponseBody
    public TableDataInfo totalList(){
        List<Map> list = alpWeeklyService.getTotalWeeklyList();
        return getDataTable(list);
    }

    /**
     * 周汇总部门级
     */
    @RequiresPermissions("system:weekly:view")
    @PostMapping("/deptList")
    @ResponseBody
    public TableDataInfo deptList(String deptName){
        String deptNames = authorityFilter.authorityDeptFilter(deptName);
        if (StringUtils.isEmpty(deptNames)) {
            List<Map> list = new ArrayList<>();
            return getDataTable(list);
        }
        List<Map> list = alpWeeklyService.getDeptWeeklyList(deptNames);
        return getDataTable(list);
    }


    /**
     * 周汇总个人级
     */
    @RequiresPermissions("system:weekly:view")
    @PostMapping("/personalList")
    @ResponseBody
    public TableDataInfo personalList(String personName){
        String personNames = authorityFilter.authorityPersonFilter(personName);
        if (StringUtils.isEmpty(personNames)) {
            List<Map> list = new ArrayList<>();
            return getDataTable(list);
        }
        List<Map> list = alpWeeklyService.getPersonalWeeklyList(personNames);
        return getDataTable(list);
    }

    /**
     * 周汇总项目级
     */
    @RequiresPermissions("system:weekly:contractList")
    @PostMapping("/contractList")
    @ResponseBody
    public TableDataInfo contractList(String contractName){
        List<Map> list = alpWeeklyService.getContractWeeklyList(contractName);
        return getDataTable(list);
    }

    /**
     * 周汇总数据穿透表跳转页
     */
    @GetMapping("/weeklyPivotTable")
    public String weeklyPivotTable(PivotRequest pivotRequest, ModelMap mmap)
    {
        mmap.put("pivotRequest", pivotRequest);
        return prefix + "/weeklyPivotTable";
    }

    /**
     * 周汇总数据穿透表
     */
    @RequiresPermissions("system:weekly:view")
    @PostMapping("/getPivotTable")
    @ResponseBody
    public TableDataInfo getPivotTable(PivotRequest pivotRequest){
        startPage();
        List<Map> list = alpWeeklyService.getWeeklyPivotTable(pivotRequest);
        return getDataTable(list);
    }

    /**
     * 周汇总项目级跳转页
     */
    @GetMapping("/toContractWeekly")
    public String toContractWeekly(ModelMap mmap)
    {
        int week = DateUtils.getWeekOfYearByF(new Date(), 5);
        mmap.put("week", week);
        return prefix + "/contractWeekly";
    }

    /**
     * 周汇总项目级
     */
    @RequiresPermissions("system:weekly:view")
    @PostMapping("/contractWeekly")
    @ResponseBody
    public TableDataInfo contractWeekly(AlpContractDetail alpContractDetail, String quarter){
        //startLayPage();
        startPage();
        List<Map> list = alpWeeklyService.getContractWeekly(alpContractDetail,quarter);
        Integer count = alpContractDetailService.getCount(alpContractDetail);
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(0);
        tableDataInfo.setRows(list);
        tableDataInfo.setTotal(count);
        return tableDataInfo;
    }
}
